﻿// -----------------------------------------------------------------------
//    This file is part of TSGE.
//
//    TSGE is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 2 of the License, or
//    (at your option) any later version.
//
//    TSGE is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with TSGE.  If not, see <http://www.gnu.org/licenses/>.
// -----------------------------------------------------------------------

namespace tsge.Controls
{
    using System;
    using System.Collections.ObjectModel;
    using System.IO;
    using System.Linq;
    using System.Windows.Media.Imaging;
    using tsge.Helpers;

    /// <summary>
    /// Terraria Image Class Implementation
    /// </summary>
    public class TerrariaImage
    {
        /// <summary>
        /// Default Constructor
        /// </summary>
        /// <param name="strImagePath"></param>
        public TerrariaImage(String strImagePath)
        {
            this.Path = strImagePath;
            this.ImageUri = new Uri(strImagePath);
            this.BmpFrame = BitmapFrame.Create(this.ImageUri);
        }

        /// <summary>
        /// Gets or sets the path of this image.
        /// </summary>
        public String Path
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the image uri of this image.
        /// </summary>
        public Uri ImageUri
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the bitmap frame of this image.
        /// </summary>
        public BitmapFrame BmpFrame
        {
            get;
            set;
        }

        /// <summary>
        /// ToString Override
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            return this.Path;
        }
    }

    /// <summary>
    /// Terraria Image List Class Implementation
    /// 
    ///     Inherits:
    ///         - ObservableCollection
    /// </summary>
    public class TerrariaImageList : ObservableCollection<TerrariaImage>
    {
        /// <summary>
        /// Internal copy of the image path.
        /// </summary>
        private String m_vPath;

        /// <summary>
        /// Internal copy of the directory info.
        /// </summary>
        private DirectoryInfo m_vDirectoryInfo;

        /// <summary>
        /// Default Constructor
        /// </summary>
        public TerrariaImageList()
        {
        }

        /// <summary>
        /// Overloaded Constructor
        /// </summary>
        /// <param name="strImagePath"></param>
        public TerrariaImageList(String strImagePath)
            : this(new DirectoryInfo(strImagePath))
        {
        }

        /// <summary>
        /// Overloaded Constructor
        /// </summary>
        /// <param name="directoryInfo"></param>
        public TerrariaImageList(DirectoryInfo directoryInfo)
        {
            this.m_vDirectoryInfo = directoryInfo;
            this.Update();
        }

        /// <summary>
        /// Updates the list of image files in the collection.
        /// 
        /// Thanks to:
        ///     Greg Beech - Natrual Sorting:
        ///     http://stackoverflow.com/a/248613
        /// </summary>
        private void Update()
        {
            var files = (from FileInfo f in this.m_vDirectoryInfo.GetFiles("*.png")
                         where f.FullName.Contains("_alt_") == false
                         select f).ToList().OrderBy(file => file, new NaturalFileInfoNameComparer());

            foreach (FileInfo f in files)
            {
                this.Add(new TerrariaImage(f.FullName));
            }
        }

        /// <summary>
        /// Gets or sets the path of this image list.
        /// </summary>
        public String Path
        {
            get { return this.m_vPath; }
            set
            {
                this.m_vPath = value;
                this.m_vDirectoryInfo = new DirectoryInfo(this.Path);
                this.Update();
            }
        }

        /// <summary>
        /// Gets or sets the directory info of this image list.
        /// </summary>
        public DirectoryInfo Directory
        {
            get { return this.m_vDirectoryInfo; }
            set
            {
                this.m_vDirectoryInfo = value;
                this.Update();
            }
        }
    }
}
